$(function(){

    $("#endMarker").text("我的收藏");

    // myLayout = $("#center").layout({
    //     spacing_open:0,
    //     spacing_closed:0,
    //     closable:false,
    //     resizable:false,
    //     west__resizable:true,
    //     west__size:256,
    // });

    $("#list-Gn .active").removeClass("active");
    $("#itemFavorite").addClass("active");

    $("#list-group").on("click","li",function(){
        var id = $(this).attr("id");
        if(id){
            $("#list-group .active").removeClass("active");
            $(this).addClass("active");
             searchFav(id)
        }
    }).on("mouseleave","li",function(){
        $(this).find(".list-group").hide();
    });

    $("#list-group").on("click",".gn",function(){
        $(this).next().show();
    });

    $("#list-group").append("<li  class='list-group-item add-folder title-s' onclick=\"addFolder();\" style='background: #F8F8F8'><i class=\"fa fa-plus-square-o fa-lg\" aria-hidden=\"true\"></i> 添加收藏夹</li>");
    var activeId = getUrlString("activeId");
    if(!isNull(activeId)){
         $("li#"+activeId).addClass("active");
        searchFav(activeId);
    }else{
        activeId = $("#list-group >li").eq(0).attr("id");
        if(!isNull(activeId)) {
            $("li#" + activeId).addClass("active");
            searchFav(activeId);
        }
    }

});



//查询收藏夹功能
function searchFav(scj_id){

      $.ajax({
        url: "/portal/user/favorite/searchGn.do",
        type: "post",
        data:{
            scj_id:scj_id
        },
        dataType:"json",
        async:false,
        timeout: sys_timeout,
        error: function (XMLHttpRequest, textStatus) {
            innerToast.error("系统访问异常："+textStatus);
        },
        success: function (responseText) {
             if(responseText.code==0){
                var gnLsit = responseText.repData.gnLsit;
                $("#favGroup").empty();
                for(var i=0;i<gnLsit.length;i++){
                    $("#favGroup").append(
                        '<div class="gn-one" data-gnid="sbns-aqsbl"> '+
                        '   <div href="#" class="gn-title gn-remove" > ' +
                        '      <a  class="fa fa-remove" title="删除"  onclick="removeFav(\''+gnLsit[i].GN_ID+'\',\''+scj_id+'\')" ></a>\n' +
                        '   </div> '+
                        '  <div class="gn-icon"> '+
                        '   <i class="fa fa-address-book"></i>'+
                        '    </div> '+
                        '    <div class="gn-title">'+gnLsit[i].GN_MC+'</div>'+
                        '</div>'
                    );
                }
             }else{
                 innerToast.error("查询收藏夹功能失败："+responseText.msg);
            }
        }
    });
}


//重命名收藏夹
function renameFolder(scj_id,name){

    flavr.confirmWithInput ('请输入收藏夹名称:',  '请输入收藏夹名称:',
        function(folderName, params,$container){
             if (folderName) {
                $.ajax({
                    url: "/portal/user/favorite/renameFolder.do",
                    type: "post",
                    data: {
                        scj_id: scj_id,
                        name: folderName
                    },
                    dataType: "json",
                    async: false,
                    timeout: sys_timeout,
                    error: function (XMLHttpRequest, textStatus) {
                        innerToast.error("系统访问异常：" + textStatus);
                    },
                    success: function (responseText) {
                        if (responseText.code == 0) {
                            window.location.href = "/portal/user/favorite/index.do?activeId=" + scj_id;
                        } else {
                            innerToast.error("保存失败：" + responseText.msg);
                        }
                    }
                });
            }
         }, function(){

         }, null, true);


}

function addFolder(){


    flavr.confirmWithInput('请输入收藏夹名称:','请输入收藏夹名称:',
        function(folderName, params,$container){
             if (folderName){
                $.ajax({
                    url: "/portal/user/favorite/addFolder.do",
                    type: "post",
                    data:{
                        name:folderName
                    },
                    dataType:"json",
                    async:false,
                    timeout: sys_timeout,
                    error: function (XMLHttpRequest, textStatus) {
                        innerToast.error("系统访问异常："+textStatus);
                    },
                    success: function (responseText) {
                        if(responseText.code==0){
                            window.location.href="/portal/user/favorite/index.do?activeId="+responseText.repData.scj_id;
                        }else{
                            innerToast.error("保存失败："+responseText.msg); // 错误提示
                        }
                    }
                });
            }
        },function(){

    }, null, true);

}


//移除收藏夹
function removeFolder(scj_id,name){

    flavr.confirm('确定删除收藏夹'+name+'及所收藏的功能?', function(){

        $.ajax({
            url: "/portal/user/favorite/removeFolder.do",
            type: "post",
            data:{
                scj_id:scj_id
            },
            dataType:"json",
            async:false,
            timeout: sys_timeout,
            error: function (XMLHttpRequest, textStatus) {
                $.messager.alert('请求失败',"系统访问异常："+textStatus);
            },
            success: function (responseText) {
                if(responseText.code==0){
                    innerToast.success("删除成功",{
                        onHidden: function () {
                            window.location.reload();
                        }
                    });
                }else{
                    innerToast.error(responseText.msg); // 错误提示
                }
            }
        });

    }, function(){

    }, null, true);


}

function topFolder(scj_id,name){

    $.ajax({
        url: "/portal/user/favorite/topFolder.do",
        type: "post",
        data:{
            name:name,
            scj_id:scj_id
        },
        dataType:"json",
        async:false,
        timeout: sys_timeout,
        error: function (XMLHttpRequest, textStatus) {
             innerToast.error("系统访问异常："+textStatus); // 错误提示
        },
        success: function (responseText) {
            if(responseText.code==0){
                window.location.href="/portal/user/favorite/index.do?activeId="+scj_id;
            }else{
                innerToast.error("保存失败："+responseText.msg);
            }
        }
    });

}


function getUrlString(name) {

    var reg = new RegExp("(^|&)" + name + "=([^&*])(&|$)", "ig");

    var url = window.location.href;

    if (url.indexOf("?") > -1) {

        url = url.substring(url.indexOf("?") + 1, url.length);
        var arr = url.split("&");
        for (var i = 0; i < arr.length; i++) {
            if (name == arr[i].split("=")[0]) {
                return arr[i].split("=")[1]
            }
        }

    }

    return null;

}

function removeFav(gn_id,scj_id){

    $.ajax({
        url: "/portal/user/favorite/removeGn.do",
        type: "post",
        data:{
            gn_id:gn_id,
            scj_id:scj_id
        },
        dataType:"json",
        async:false,
        timeout: sys_timeout,
        error: function (XMLHttpRequest, textStatus) {
            innerToast.error("系统访问异常："+textStatus);
        },
        success: function (responseText) {
            if(responseText.code==0){
                window.location.href="/portal/user/favorite/index.do?activeId="+scj_id;
            }else{
                 innerToast.error("保存失败："+responseText.msg);
            }
        }
    });

}

